﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using Project.dalSql;
using System.Data.SqlClient;
using System.Data;
using System.Web.SessionState;
using WISD.CarFriend.BLL.OA;
using WISD.Common;
using WISD.CarFriend.Model.CFModel;

namespace WISD.CarFriend.Web.Admin.Control
{
    /// <summary>
    /// Summary description for Consume
    /// </summary>
    public class Consume : IHttpHandler, IReadOnlySessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            var Action = NullHelper.NullProcess<string>(context.Request["Action"], "");
            var MemberCardID = NullHelper.NullProcess<string>(context.Request["MemberCardID"], "");
            var ConsumeTime = NullHelper.NullProcess<DateTime>(context.Request["ConsumeTime"], DateTime.Parse("1888/01/01"));
            var ConsumeLocation = NullHelper.NullProcess<string>(context.Request["ConsumeLocation"], "");
            var ShopName = NullHelper.NullProcess<string>(context.Request["ShopName"], "");
            var ShopLeader = NullHelper.NullProcess<string>(context.Request["ShopLeader"], "");
            var ConsumeIDItem = NullHelper.NullProcess<int>(context.Request["ConsumeIDItem"], 0);
            var ConsumeAmount = NullHelper.NullProcess<int>(context.Request["ConsumeAmount"], 0);
            var PaidAmount = NullHelper.NullProcess<int>(context.Request["PaidAmount"], 0);
            var Memo = NullHelper.NullProcess<string>(context.Request["Memo"], "");
            var Reviser = context.Session["UserCode"].ToString();
            var ReviseDate = System.DateTime.Now;

            int PageSize = NullHelper.NullProcess<int>(context.Request["PageSize"], 0);
            int PageIndex = NullHelper.NullProcess<int>(context.Request["CurrentPage"], 0);
            var Condition = NullHelper.NullProcess<string>(context.Request["Condition"], "");
            var Main = NullHelper.NullProcess<string>(context.Request["Main"], "");

            //加载所有部门的信息
            if (Action.Equals("AddEntity"))
            {
                T_CF_OA_Consume item = new T_CF_OA_Consume();
                item.MemberCardID = MemberCardID;
                item.ConsumeTime = ConsumeTime;
                item.ConsumeLocation = ConsumeLocation;
                item.ShopName = ShopName;
                item.ShopLeader = ShopLeader;
                item.ConsumeIDItem = ConsumeIDItem;
                item.ConsumeAmount = ConsumeAmount;
                item.PaidAmount = PaidAmount;
                item.Memo = Memo;
                item.Reviser = Reviser;
                item.ReviseDate = ReviseDate;

                if (ConsumeModule.AddEntity(item).Equals("Success"))
                {
                    context.Response.Write("Success");
                }
                else
                {
                    context.Response.Write("Failure");
                }
                context.Response.End();
            }
            if (Action.Equals("DeleteEntity"))
            {
                context.Response.Write(ConsumeModule.DeleteEntity(Main));
                context.Response.End();
            }
            if (Action.Equals("EditEntity"))
            {
                T_CF_OA_Consume item = new T_CF_OA_Consume();
                item.MemberCardID = MemberCardID;
                item.ConsumeTime = ConsumeTime;
                item.ConsumeLocation = ConsumeLocation;
                item.ShopName = ShopName;
                item.ShopLeader = ShopLeader;
                item.ConsumeIDItem = ConsumeIDItem;
                item.ConsumeAmount = ConsumeAmount;
                item.PaidAmount = PaidAmount;
                item.Memo = Memo;
                item.Reviser = Reviser;
                item.ReviseDate = ReviseDate;

                if (ConsumeModule.EditEntity(item).Equals("Success"))
                {
                    context.Response.Write("Success");
                }
                else
                {
                    context.Response.Write("Failure");
                }
                context.Response.End();
            }

            if (Action.Equals("Init"))
            {
                context.Response.Write(ConsumeModule.GetData(PageSize, PageIndex, Condition));
                context.Response.End();
            }
            if (Action.Equals("QueryInit"))
            {
                context.Response.Write(ConsumeModule.GetQueryData(PageSize, PageIndex, Condition));
                context.Response.End();
            }
            if (Action.Equals("GetOne"))
            {
                context.Response.Write(ConsumeModule.GetOne(Main));
                context.Response.End();
            }
            if (Action.Equals("GetCardInfo"))
            {
                context.Response.Write(ConsumeModule.GetCardInfo(MemberCardID));
                context.Response.End();
            }

            if (Action == "GetCardDetail")
            {
                context.Response.Write(ConsumeModule.GetCardDetail(PageSize, PageIndex, Condition));
                context.Response.End();
            }
            if (Action == "GetConsumeLocation")  //获取消费地点
            {
                string ConsumeLocationStr = "";
                var PageID = NullHelper.NullProcess<string>(context.Request["PageID"], "");
                if (context.Session["UserType"].ToString() == "0" || PageID != "")  //超级管理员
                {
                    ConsumeLocationStr = ConsumeModule.GetShopAddress("");
                }
                else
                {
                    ConsumeLocationStr = ConsumeModule.GetShopAddress(context.Session["UserID"].ToString());
                }
                context.Response.Write(ConsumeLocationStr);
                context.Response.End();
            }
            if (Action == "GetConsumeQuery")
            {
                string ConsumeLocationStr = "";
                if (context.Session["UserType"].ToString() != "0")
                {
                    ConsumeLocationStr = ConsumeModule.GetShopAddress(context.Session["UserID"].ToString());
                }
                context.Response.Write(ConsumeLocationStr);
                context.Response.End();
            }
            if (Action == "GetMemberConsumeItem")
            {
                context.Response.Write(ConsumeModule.GetConsumeItemByMember(MemberCardID));
                context.Response.End();
            }
            if (Action == "GetConsumeHistory")  //消费历史记录
            {
                context.Response.Write(ConsumeModule.GetConsumeHistory(PageSize, PageIndex, Condition));
                context.Response.End();
            }
            if (Action == "CheckMemberPWD")  //检查输入的会员密码是否正确(如果密码正确则返回该会员卡的是否过期值，否则返回Failure)
            {
                var MemberPWD = NullHelper.NullProcess<string>(context.Request["MemberPWD"], "");
                DataTable dt = ConsumeModule.CheckMemberPWD(MemberCardID, MemberPWD);
                try
                {
                    if (dt != null && dt.Rows.Count != 0)
                    {
                        DateTime ExpirationDate = DateTime.Parse(dt.Rows[0]["ExpirationDate"].ToString());
                        if (ExpirationDate < DateTime.Now)
                        {
                            context.Response.Write("1"); //过期
                            context.Response.End();
                        }
                        else
                        {
                            context.Response.Write("0");
                            context.Response.End();
                        }
                    }
                    else
                    {
                        context.Response.Write("Failure");
                        context.Response.End();
                    }
                }
                catch (Exception)
                { }
            }
            if (Action == "GetConsumeInfo")  //新增回访时读取基本数据
            {
                var ConsumeID = NullHelper.NullProcess<string>(context.Request["ConsumeID"], "");

                context.Response.Write(ConsumeModule.GetConsumeInfo(ConsumeID));
                context.Response.End();
            }
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}